package core

import grails.transaction.Transactional
import grails.validation.ValidationException

@Transactional
class PersonService {

    def serviceMethod() {

    }
	
	def save(params)
	{
		def personId=params.long('id')
		def person
		if(personId)
		{
			//edit
			person= Person.get(personId)
			if(!person)
			{
				person=new Person()
				person.errors.reject('notvalidpersonId')
				throw new ValidationException('person id no valid',person.errors)
				
			}
			else{
				
				person=new Person()
			}
			if(person.findByFirstNameAndLastNameAndIdNotEqual(params.firstName,params.lastName,personId))
			{
				person.errors.reject('person with the same name already exist')
				throw new ValidationException('person with the same name already exist',person.errors)
				
			}
			person.properties=params
			if(!person.save())
			{
				throw new ValidationException('validation error',person.errors)
				
			}
			return person
			
			
		}
		
	}
}
